﻿using facebook.businuss.netcore.sdk.Model;
using facebook.businuss.netcore.sdk.Model.Response;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace facebook.businuss.netcore.sdk.Sdk
{
    public class APIRequestGetAdAccounts:APIRequest
    {
        public APIRequestGetAdAccounts(APIContext context, String nodeId, String endpoint, String method, List<String> paramNames)
        : base(context, nodeId, endpoint, method, paramNames)
        {

        }

        public APIRequestGetAdAccounts(String nodeId, APIContext context)
            : this(context, nodeId, "/adaccounts", "GET", null)
        {

        }

        public List<AdAccountInfo> execute()
        {
            ResponseWrapper rw = base.execute(new Dictionary<string, object>());
            var result = new List<AdAccountInfo>();
            bool haveNext = false;

            var temData = rw == null ? null : JsonConvert.DeserializeObject<PageBaseResponse<List<AdAccountInfo>>>(rw.Body);
            haveNext = temData!=null&&!string.IsNullOrEmpty(temData.Next);
            result.AddRange(temData==null?null:temData.Data);

            while (haveNext)
            {
                base.execute(temData.Next);
                var ps = rw == null ? null : JsonConvert.DeserializeObject<PageBaseResponse<List<AdAccountInfo>>>(rw.Body);

                haveNext = ps != null && !string.IsNullOrEmpty(ps.Next);
                result.AddRange(ps==null?null:ps.Data);
            }

            return result.Where(s=>s.mAccountStatus==1).ToList();
        }

        public APIRequestGetAdAccounts requestField(String field)
        {
            this.requestField(field, true);
            return this;
        }

        public APIRequestGetAdAccounts requestField(String field, bool value)
        {
            base.requestFieldInternal(field, value);
            return this;
        }
    }
}
